# Title     : TODO
# Objective : TODO
# Created by: Administrator
# Created on: 2019/7/24

library(plyr)
library(gridExtra)
library(scales)
library(egg)
library(VennDiagram)
library(optparse)
library(magrittr)
library(tidyverse)

createWhenNoExist <- function(f) {
  !dir.exists(f) && dir.create(f)
}

option_list <- list(
  make_option("--config", default = "config.csv", type = "character", help = "config file")
)
opt <- parse_args(OptionParser(option_list = option_list))

configData <- read.csv(opt$config, header = F, stringsAsFactors = F) %>%
  set_colnames(c("arg", "value")) %>%
  column_to_rownames("arg")

configData

pValue <- configData["diffMethod.kwP", "value"] %>%
  as.numeric()
fdr <- configData["diffMethod.kwFdr", "value"] %>%
  as.numeric()

pValue
fdr

parent <- "./"
createWhenNoExist(parent)

finalDf <- read_csv("AllMet_Test.csv") %>%
  rowwise() %>%
  do({
       result <- as.data.frame(.)
       p <- result[1, "P"]
       q <- result[1, "FDR"]
       value <- if (p < pValue & q <= fdr) {
         1
       }else 0
       result$IS_Uni_P_Sig <- value
       result
     }) %>%
  select(c("Metabolite", "IS_Uni_P_Sig")) %>%
  filter(IS_Uni_P_Sig == 1)

finalDf
outFileName <- paste0(parent, "/Markers.txt")
write_tsv(finalDf, outFileName)





